package com.web709.controller;

import com.web709.common.R;
import com.web709.entity.MomentImage;
import com.web709.entity.UserMoment;
import com.web709.service.MomentImageService;
import com.web709.service.UserMomentService;
import com.web709.util.JwtUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

// MomentController.java
@RestController
@RequestMapping("/moments")
@Api(tags = "朋友圈管理")
@RequiredArgsConstructor
public class MomentController {

    @Autowired
    private  UserMomentService userMomentService;


    @PostMapping("/add")
    @ApiOperation("添加朋友圈")
    @ApiImplicitParam(name = "token", required = false, paramType = "header", dataType = "String")
    public R add(@RequestBody UserMoment userMoment,HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Integer userId = (Integer) claims.get("userId");
        userMoment.setUserId(userId);
        userMomentService.add(userMoment);
        return R.success("发表成功");
    }

    //使用分页返回朋友圈数据
    @GetMapping("/list")
    @ApiOperation(value = "分页返回朋友圈数据",notes = "返回个人朋友圈数据")
    @ApiImplicitParam(name = "token", required = false, paramType = "header", dataType = "String")
    public R list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                              @RequestParam(value = "limit",defaultValue = "10") Integer limit,
                              HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Integer userId = (Integer) claims.get("userId");
        return R.success(userMomentService.list(userId,page,limit));
    }

    //使用分页返回所有的朋友圈数据
    @GetMapping("/allList")
    @ApiOperation(value = "分页返回所有朋友圈数据",notes = "返回所有朋友圈数据")
    @ApiImplicitParam(name = "token", required = false, paramType = "header", dataType = "String")
    public R<List<UserMoment>> allList(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        return R.success(userMomentService.allList(page, limit, title, startTime, endTime));
    }
    //删除朋友圈
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除朋友圈",notes = "删除朋友圈")
    @ApiImplicitParam(name = "token", required = false, paramType = "header", dataType = "String")
    public R<String> delete(@RequestParam("id") Integer id){
        userMomentService.delete(id);
        return R.success("删除成功");
    }

    //修改朋友圈
    @PutMapping("/update")
    @ApiOperation(value = "修改朋友圈",notes = "修改朋友圈")
    @ApiImplicitParam(name = "token", required = false, paramType = "header", dataType = "String")
    public R<String> update(@RequestBody UserMoment userMoment){
        userMomentService.update(userMoment);
        return R.success("修改成功");
    }

}
